#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "esp_log.h"
#include "led.h"

typedef struct {
    int duration;
    int count;
    int id;
} led_params;

void led_blink(void *task_params)
{

    led_params *params = (led_params *)task_params;
    int count = params->count;
    int duration = params->duration;
    int id = params->id;
    ESP_LOGI("TASK", "LED BLINK TASK: %d,...%d(%d)\n", count, duration, id);

    for (int i = 0; i < count; i++)
    {
        led_on();
        ESP_LOGI("TASK", "Blink ON%d,%d\n", i, id);
        vTaskDelay(pdMS_TO_TICKS(duration));
        led_off();
        ESP_LOGI("TASK", "Blink OFF%d,%d\n", i, id);
        vTaskDelay(pdMS_TO_TICKS(duration));
    }
}

void run_led_blink_task(int count, int duration, int id)
{
    ESP_LOGI("TASK", "Run led blink task\n");
    led_params params = {
        .duration = duration,
        .count = count,
        .id = id,
    };
    init_led_GPIO(LED_GPIO_NUM);
    xTaskCreatePinnedToCore(led_blink, "blink task", 2048, (void *)&params, 3, NULL, 0);
}